﻿using System.Web.Mvc;
using AutoMapper;
using CNav.Business;
using CNav.Business.Services;
using CNav.Common;
using CNav.Menu;
using CNav.Model;
using CNav.Support;
using Microsoft.Practices.Unity;
using System.Web.Security;

namespace CNav.Controllers
{
    public class BaseController : Controller
    {
        [Dependency]
        protected IServiceContainer Service { get; set; }

        protected override void OnAuthorization(AuthorizationContext filterContext)
        {
            if (User.Identity.IsAuthenticated)
            {
                if (LoginUser == null)
                {
                    FormsAuthentication.SignOut();
                }
            }
            base.OnAuthorization(filterContext);
        }

        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var displayname = GetDisplayName();
            ViewData["DisplayName"] = displayname;

            var siteMenuManager = new MenuManager();
            if (User.Identity.IsAuthenticated)
            {
                ViewBag.SiteLinks = siteMenuManager.GetSiteMenuItems(Request.RequestContext);

                if (IsBusinessUser)
                {
                    ViewBag.BusinesSiteLinks = siteMenuManager.GetBusinesSiteLinks(Request.RequestContext);
                }
            }

            base.OnActionExecuting(filterContext);
        }

        [Authorize]
        public virtual JsonResult CurrentUser()
        {
            SessionUser userProfile = LoginUser;
            return Json(userProfile, "application/json", JsonRequestBehavior.AllowGet);
        }

        private SessionUser loginUser;
        protected virtual SessionUser LoginUser
        {
            get
            {
                if (loginUser != null)
                    return loginUser;

                var sesssionUser = Session[Constant.SessionUser];
                if (sesssionUser != null)
                {
                    loginUser = (SessionUser)sesssionUser;
                    return loginUser;
                }

                if (User.Identity.IsAuthenticated)
                {
                    var userProfile = Service.Get<IUserAccountService>().GetUserByEmail(User.Identity.Name);

                    if (userProfile.Data != null)
                    {
                        loginUser = Mapper.Map<SessionUser>(userProfile.Data);
                        Session[Constant.SessionUser] = loginUser;
                        return loginUser;
                    }
                }
                return null;
            }
        }

        protected virtual bool IsBusinessUser
        {
            get
            {
                var userProfile = LoginUser;
                if (userProfile == null)
                    return false;

                return userProfile.IsBusinessAdmin;
            }
        }

        private string GetDisplayName()
        {
            var displayname = "Guest";

            var userProfile = LoginUser;
            if (userProfile != null)
            {
                displayname = userProfile.Email;

                if (userProfile.IsComplete())
                {
                    if (userProfile.IsBusinessAdmin)
                        displayname = userProfile.FirstName;
                    else
                        displayname = string.Format("{0} {1}, {2}", userProfile.Title, userProfile.FirstName, userProfile.LastName);
                }
            }
            return displayname;
        }
    }
}